home *** CD-ROM | disk | FTP | other *** search
- /* Send and receive IP datagrams on serial lines. Compatible with SLIP
- * under Berkeley Unix.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include "global.h"
- #include "mbuf.h"
- #include "slip.h"
- #include "iface.h"
- #include "asy.h"
- #include "timer.h"
- #include "ip.h"
- #include "arc.h"
-
- static int slipq(struct interface *, struct mbuf *);
- static void asy_start(struct interface *);
- static struct mbuf *slip_encode(struct mbuf *);
- static struct mbuf *slip_decode(struct interface *, char);
-
- /* Send routine for point-to-point slip
- * This is a trivial function since there is no slip link-level header
- */
- int slip_send(struct mbuf *data, struct interface *interface,
- int32 gateway, char precedence, char delay,
- char throughput, char reliability)
- {
- gateway = gateway;
- precedence = precedence;
- delay = delay;
- throughput = throughput;
- reliability = reliability;
-
- if(interface == NULLIF){
- free_p(data);
- return -1;
- }
- return (*interface->raw)(interface,data);
- }
- /* Send a raw slip frame -- also trivial */
- int slip_raw(struct interface *interface, struct mbuf *data)
- {
- /* Queue a frame on the slip output queue and start transmitter */
- return slipq(interface,data);
- }
- /* Encode a raw packet in slip framing, put on link output queue, and kick
- * transmitter
- */
- static int slipq(struct interface *interface, struct mbuf *data)
- {
- register struct slip *sp;
- struct mbuf *bp;
-
- if((bp = slip_encode(data)) == NULLBUF)
- return -1;
-
- sp = interface->slip;
- enqueue(&sp->sndq,bp);
- sp->sndcnt++;
- if(sp->tbp == NULLBUF)
- asy_start(interface);
- return 0;
- }
- /* Start output, if possible, on asynch device dev */
- static void asy_start(struct interface *interface)
- {
- register struct slip *sp;
- char *buffer;
- int n;
-
- sp = interface->slip;
-
- if (sp->tbp == NULLBUF) {
- if (sp->sndq == NULLBUF)
- return; /* No work */
-
- sp->tbp = dequeue(&sp->sndq);
- sp->sndcnt--;
- }
-
- if ((n = (*interface->put)(interface->dev,sp->tbp->data,sp->tbp->cnt)) < sp->tbp->cnt)
- {
- buffer = malloc(n);
- pullup(&sp->tbp, buffer, n);
- free(buffer);
- }
- else
- {
- free_p(sp->tbp);
- sp->tbp = NULLBUF;
- }
- }
- /* Encode a packet in SLIP format */
- static struct mbuf *slip_encode(struct mbuf *bp)
- {
- struct mbuf *lbp; /* Mbuf containing line-ready packet */
- register char *cp;
- char c;
-
- /* Allocate output mbuf that's twice as long as the packet.
- * This is a worst-case guess (consider a packet full of FR_ENDs!)
- */
- lbp = alloc_mbuf(2*len_mbuf(bp) + 2);
- if(lbp == NULLBUF){
- /* No space; drop */
- free_p(bp);
- return NULLBUF;
- }
- cp = lbp->data;
-
- /* Flush out any line garbage */
- *cp++ = FR_END;
-
- /* Copy input to output, escaping special characters */
- while(pullup(&bp,&c,1) == 1){
- switch(uchar(c)){
- case FR_ESC:
- *cp++ = FR_ESC;
- *cp++ = T_FR_ESC;
- break;
- case FR_END:
- *cp++ = FR_ESC;
- *cp++ = T_FR_END;
- break;
- default:
- *cp++ = c;
- }
- }
- *cp++ = FR_END;
- lbp->cnt = cp - lbp->data;
- return lbp;
- }
- /* Process incoming bytes in SLIP format
- * When a buffer is complete, return it; otherwise NULLBUF
- */
- static struct mbuf *slip_decode(struct interface *interface, char c)
- {
- struct mbuf *bp;
- register struct slip *sp;
-
- sp = interface->slip;
- switch(uchar(c)){
- case FR_END:
- bp = sp->rbp;
- sp->rbp = NULLBUF;
- sp->rcnt = 0;
- return bp; /* Will be NULLBUF if empty frame */
- case FR_ESC:
- sp->escaped = 1;
- return NULLBUF;
- }
- if(sp->escaped){
- /* Translate 2-char escape sequence back to original char */
- sp->escaped = 0;
- switch(uchar(c)){
- case T_FR_ESC:
- c = FR_ESC;
- break;
- case T_FR_END:
- c = FR_END;
- break;
- default:
- sp->errors++;
- break;
- }
- }
- /* We reach here with a character for the buffer;
- * make sure there's space for it
- */
- if(sp->rbp == NULLBUF){
- /* Allocate first mbuf for new packet */
- if((sp->rbp1 = sp->rbp = alloc_mbuf(SLIP_ALLOC)) == NULLBUF)
- return NULLBUF; /* No memory, drop */
- sp->rcp = sp->rbp->data;
- } else if(sp->rbp1->cnt == SLIP_ALLOC){
- /* Current mbuf is full; link in another */
- if((sp->rbp1->next = alloc_mbuf(SLIP_ALLOC)) == NULLBUF){
- /* No memory, drop whole thing */
- free_p(sp->rbp);
- sp->rbp = NULLBUF;
- sp->rcnt = 0;
- return NULLBUF;
- }
- sp->rbp1 = sp->rbp1->next;
- sp->rcp = sp->rbp1->data;
- }
- /* Store the character, increment fragment and total
- * byte counts
- */
- *sp->rcp++ = c;
- sp->rbp1->cnt++;
- sp->rcnt++;
- return NULLBUF;
- }
- /* Process SLIP line I/O */
- int doslip(struct interface *interface)
- {
- char c;
- struct mbuf *bp;
- int16 dev;
-
- dev = interface->dev;
- /* Process any pending input */
- while((*interface->get)(dev,&c,1) != 0)
- {
- if((bp = slip_decode(interface,c)) != NULLBUF)
- (*interface->slip->recv)(interface,bp);
- }
- /* Kick the transmitter if it's idle */
- asy_start(interface);
- return 0;
- }
- /* Unwrap incoming SLIP packets -- trivial operation since there's no
- * link level header
- */
- void slip_recv(struct interface *interface, struct mbuf *bp)
- {
- interface = interface;
- /* By definition, all incoming packets are "addressed" to us */
- ip_route(bp,0);
- }
-